package com.surfilter.platform.base.exception;

import org.apache.log4j.Logger;

import com.surfilter.platform.base.model.ResultCode;

public abstract class ExceptionBase extends RuntimeException {

	private static final long serialVersionUID = -5698284651780400311L;

	public static final String LOG_BASE = "LOG";
	public static final String LOG_SYSTEM = "SYSTEM";
	public static final String LOG_MODULE = "MODULE";

	private Logger logger = getLogger();
	
	protected int errorCode = ResultCode.FAIL;
	protected String customMsg;

	public ExceptionBase(String msg) {
		super(msg);
		this.customMsg = msg;
		logger.error(msg);
	}

	public ExceptionBase(Throwable t) {
		super(t);
		Throwable c = t.getCause();
		while(c != null){
			if(c instanceof ExceptionBase){
				String cMsg = ((ExceptionBase)c).getCustomMsg();
				if(cMsg!= null){
					this.customMsg = cMsg;
					break;
				}
			}
			c = c.getCause();
		}
		logger.error(t);
	}

	public ExceptionBase(String msg, Throwable t) {
		super(msg, t);
		this.customMsg = msg;
		logger.error(msg,t);
	}

	public int getErrorCode() {
		return errorCode;
	}

	public String getCustomMsg() {
		return customMsg;
	}

	protected abstract void setErrorCode(int code);
	
	protected abstract Logger getLogger();
}
